package com.Controller;

import com.Service.StudentService;
import com.dao.StuDao;
import com.domain.Student;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class StudentServlet {
    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/selectByPage",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String selectByPage(@RequestParam("currentPage") String currentPage ,@RequestParam("pageSize") String pageSize) throws JsonProcessingException {
        Page page = studentService.pageQuery(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        PageInfo<List<Student>> info = new PageInfo<>(page);
        ObjectMapper om = new ObjectMapper();
        return  om.writeValueAsString(info);

    }


    @RequestMapping(value ="/addStudent",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String addStudent(Student student){
        studentService.addStu(student);
        return "true";
    }
    @RequestMapping(value = "/delStudent",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String delStudent(Integer id){
        studentService.delStu(id);
        return "true";
    }
    @RequestMapping(value = "updateStudent",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String updateStudent(Student student){
        studentService.updateStu(student);
        return "true";
    }

    @RequestMapping(value = "findStu",produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String findStu(@RequestParam("currentPage") Integer currentPage ,@RequestParam("pageSize") Integer pageSize,Student student) throws JsonProcessingException {
        Page page = studentService.findStu(student, currentPage, pageSize);
        PageInfo<List<Student>> info = new PageInfo<>(page);
        ObjectMapper om = new ObjectMapper();
       return  om.writeValueAsString(info);
    }
}
